<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Content extends CI_Controller {
	public function __construct(){
		parent::__construct();
		//连接数据库
		$this->db = $this->load->database ( "default", TRUE );
		//读取dilicms的配置属性
		$this->siteData = $this->db->get($this->db->dbprefix('site_settings'))->row_array();
		$this->tableName=$this->db->dbprefix('u_m_content');
		$this->categoryTableName=$this->db->dbprefix('u_c_category');
	}
	public function index()
	{
		//$this->pageData["title"]=$this->categoryName;
		$_id=$this->input->get("id");
		$_category=$this->input->get("category");
		$_id=(is_numeric($_id))?$_id:1;
		$_category=(is_numeric($_category))?$_category:1;
		$_id=($_id<=0)?1:$_id;
		$_category=($_category<=0)?1:$_category;
		$resData=$this->__getContentData($_id,$_category);
		$this->pageData["db"]=$resData;
		$this->pageData["tongzhi"]=$this->__getAside(26);
		$this->pageData["center_new"]=$this->__getAside(12);
		$this->pageData["category_name"]=$this->__getCategoryName($_category);
		$this->load->view('content',$this->pageData);
	}
	
	//获取边栏
	private function __getAside($_id){
		$whereCondition="where ";
		$whereCondition.=($_id!=0)?("category_name={$_id}"):("1=1");
		$sql="select * from {$this->tableName} {$whereCondition}";
		$sql.=" order by id desc limit 0,1";
		$resData=$this->db->query($sql)->result_array();
		return $resData;
	}
	
	//读取新闻具体数据（内容页）
	private function __getContentData($_id,$_category){
		$whereCondition="where id  ={$_id} and category_name={$_category}";
		$sql="select * from {$this->tableName}  {$whereCondition}";
		//$hitSql="update {$this->tableName} set hit=hit+1 where id={$_id}";
		$resData=$this->db->query($sql)->row_array();
		//$this->db->query($hitSql);
		return $resData;
	}	
	//获取栏目名称
	private function __getCategoryName($_category){
		$whereCondition="where classid  = {$_category}";
		$sql="select category_name from {$this->categoryTableName}  {$whereCondition}";
		$resData=$this->db->query($sql)->row_array();
		$categoryName=(sizeof($resData)==0)?-1:$resData["category_name"];
		return $categoryName;
	}
}